﻿// //文档就绪函数
$(function () {
    fantian();
})
var obj = {
    craftManageId: '',
    craftManagenumbering: "",
    craftManageName: "",
    craftManageState: "",
    craftManageFinishName: "",
    craftManageDate: "",
    craftManageAddName: "",
    craftManageIsDel: "",
    createName: "",
    createTime: "",
};
function fantian() {
    layui.use(['layer', 'form', 'util', 'table'], function () {
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;
        var util = layui.util;
        var table = layui.table;

        return new Promise((resolve, reject) => {
            
            // 获取要编辑的用户ID
            const craftManageId = localStorage.getItem("craftManageId");
            console.log(craftManageId);
            if (craftManageId) {
                // 发送AJAX请求获取数据
                $.ajax({
                    type: "get",
                    url: "/Craft/GetCraftBackFill",
                    data: {
                        craftManageId: craftManageId,
                        token: localStorage.getItem("token")
                    },
                    success: (res) => {
                      
                            const { data } = JSON.parse(res);
                            //console.log(data)
                            if (data) {
                                const { storageProductData } = data;
                                var storage = data;
                                console.log(storage);
                                console.log(storageProductData);
                                // 反填表单的函数
                                fillForm(storage);
                                
                                resolve(storage, storageProductData);
                            } else {
                                layui.layer.msg(res.message, { icon: 2 });
                                reject(res.message);
                            }
                       
                    },
                   
                });
            } 
        });

        
        // 反填表单的函数
        function fillForm(storage) {

            
            $('#craftManagenumbering').val(storage.craftManagenumbering);
            $('#craftManageName').val(storage.craftManageName);
            var craftState = storage.craftManageState
            $("[name='craftManageState']").each(function () {
                if (craftState.toString() == $(this).val()) {
                    $(this).prop("checked",true)
                }
            })
            $('#craftManageFinishName').val(storage.craftManageFinishName);
            const date = new Date(storage.craftManageDate);
            const year = date.getFullYear();
            const month = ("0" + (date.getMonth() + 1)).slice(-2);
            const day = ("0" + date.getDate()).slice(-2);
            $('#craftManageDate').val(year + "-" + month + "-" + day);
            $('#craftManageAddName').val(storage.craftManageAddName);

            console.log(storage.craftManageId)
            var dd = storage.craftManageId
            console.log(dd);
            obj.craftManageId = storage.craftManageId;
            obj.craftManagenumbering = storage.craftManagenumbering;
            obj.craftManageName = storage.craftManageName;
            obj.craftManageState = storage.craftManageState;
            obj.craftManageFinishName = storage.craftManageFinishName;
            obj.craftManageDate = storage.craftManageDate;
            obj.craftManageAddName = storage.craftManageAddName;
            obj.craftManageIsDel = storage.craftManageIsDel;
            obj.createName = storage.createName;
            obj.createTime = storage.createTime;
            console.log(obj)
            // 重新渲染表单元素（如果有）
            //form.render();
        }
    })
}




//修改工艺
var obj1 = {
    craftManageId: '',
    craftManagenumbering: "",
    craftManageName: "",
    craftManageState: "",
    craftManageFinishName: "",
    craftManageDate: "",
    craftManageAddName: "",
    craftManageIsDel: "",
    createName: "",
    createTime: "",
    
};
layui.use(['layer', 'form', 'util', 'table'], function () {
    var layer = layui.layer;
    var $ = layui.jquery;
    var form = layui.form;
    var util = layui.util;
    var table = layui.table;

    form.on('submit(demo2)', function (data) {
        
        var field = data.field;
        //var checkStatus = table.checkStatus('storageProduct');
        //field.storageProducts = checkStatus.data.map(function (item) {
        //    return item;
        //});
        console.log(field);//field.products

        obj1.craftManageId = obj.craftManageId
        obj1.craftManagenumbering = $('#craftManagenumbering').val();
        obj1.craftManageName = $('#craftManageName').val();
        obj1.craftManageState = $('#craftManageState').val();
        obj1.craftManageFinishName = $('#craftManageFinishName').val();
        obj1.craftManageDate = $('#craftManageDate').val();
        obj1.craftManageAddName = $('#craftManageAddName').val();
        obj1.craftManageIsDel = field.craftManageIsDel
        obj1.createName = obj.createName
        obj1.createTime = obj.createTime
        console.log(obj1)
        //显示填写结果，仅作演示用
        //debugger
        $.ajax({

            url: '/Craft/GetCraftUpdate',
            type: 'post',
            data: {
                dto: obj1,
                token: localStorage.getItem("token"),
            },
            success: function (res) {
                
                var res = JSON.parse(res)
                console.log(obj1);
                console.log(res);
                if (res.data > 0) {
                    layer.alert("修改成功");
                    //跳转页面
                    setTimeout(function () {
                        location.href = '/Craft/CraftShow'
                    }, 2000);
                } else {
                    layer.alert("修改失败");
                }
            }
        });

        return false; // 阻止默认 form 跳转
    });
});
